package com.spring.mvc.session.client;

import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;

@SuppressWarnings("deprecation")
public class HttpSessionWapper implements HttpSession {
	
	private SessionService service;
	private String groupID="0";
	private String sessionID="";
	private HttpSession session;

	public HttpSessionWapper(String sessionId, SessionService sessionService,
			HttpSession session) {
		this.session=session;
		this.service=sessionService;
		this.sessionID=sessionId;
	}

	public Object getAttribute(String name) {
		Object old=session.getAttribute(name);
		if(old==null){
			old=this.service.getAttribute(this.sessionID, name, groupID);
			if(old!=null){
				session.setAttribute(name, old);
			}
		}
		return old;
	}

	@SuppressWarnings("unchecked")
	public Enumeration getAttributeNames() {
		return this.session.getAttributeNames();
	}

	public long getCreationTime() {
		return session.getCreationTime();
	}

	public String getId() {
		return this.sessionID;
	}

	public long getLastAccessedTime() {
		return this.session.getLastAccessedTime();
	}

	public int getMaxInactiveInterval() {
		return this.session.getMaxInactiveInterval();
	}

	public ServletContext getServletContext() {
		return this.session.getServletContext();
	}

	public HttpSessionContext getSessionContext() {
		return this.session.getSessionContext();
	}

	public Object getValue(String name) {
		return getAttribute(name);
	}

	public String[] getValueNames() {
		return this.session.getValueNames();
	}

	public void invalidate() {
		this.service.invalidate(this.sessionID, groupID);
		this.session.invalidate();
	}

	public boolean isNew() {
		return this.session.isNew();
	}

	public void putValue(String name, Object value) {
		setAttribute(name, value);
	}

	public void removeAttribute(String name) {
		this.session.removeAttribute(name);
		this.service.removeAttribute(this.sessionID, name, this.groupID);
	}

	public void removeValue(String name) {
		removeAttribute(name);
	}

	public void setAttribute(String name, Object value) {
		this.session.setAttribute(name, value);
		this.service.setAttribute(this.sessionID, name, value, this.groupID);
	}

	public void setMaxInactiveInterval(int interval) {
		this.session.setMaxInactiveInterval(interval);
		this.service.setMaxInactiveInterval(this.sessionID, interval, this.groupID);
	}

}
